package uk.co.q3c.deplan.server.unit.dao;

import java.util.Calendar;

import org.apache.log4j.Logger;
import org.testng.Assert;
import org.testng.annotations.Test;

import uk.co.q3c.deplan.client.domain.dag.ChangeInvalidException;
import uk.co.q3c.deplan.client.domain.dag.CycleDetectedException;
import uk.co.q3c.deplan.client.domain.resource.BaseCalendar;
import uk.co.q3c.deplan.client.domain.resource.CalendarAdjustment;
import uk.co.q3c.deplan.client.domain.resource.PersistenceReferenceException;
import uk.co.q3c.deplan.server.util.Q3Calendar;

/**
 * {@link BaseCalendarDao_BigTable}
 * 
 * @author DSowerby 18 Nov 2008
 * 
 */
public class BaseCalendarDao_BigTable_UT extends LocalDatastoreTestCase {

	transient protected final Logger logger = Logger.getLogger(getClass().getName());

	@Override
	protected void beforeMethod() throws Exception {
		super.beforeMethod();

		// baseCalendarDao.deleteAll();
	}

	@Override
	protected void afterMethod() throws Exception {
		connection.taskService()
				.closeSession();
		super.afterMethod();
	}

	@Test
	public void testSaveDelete() throws CycleDetectedException, ChangeInvalidException, PersistenceReferenceException {
		Calendar cal = new Q3Calendar();
		cal.set(2008, 4, 1);
		BaseCalendar baseCalendar;
		baseCalendar = new BaseCalendar(connection);
		// check cal is set to a working day
		Assert.assertEquals(baseCalendar.getWorkingTime(cal.getTime()), 450);

		// make that day zero
		baseCalendar.addAdjustment(2008, 4, 1, true, 0);
		CalendarAdjustment cadj = baseCalendar.getAdjustment(cal.getTime());
		Assert.assertEquals(cadj.getAdjustment(), 0);
		testdao.save(baseCalendar);

		Assert.assertEquals(baseCalendar.getWorkingTime(cal.getTime()), 0);
		Assert.assertEquals(baseCalendar.adjustmentCount(), 1);
		testdao.save(baseCalendar);
		Assert.assertEquals(baseCalendar.adjustmentCount(), 1);
		// 
		// baseCalendar = null;

		// check it is there
		BaseCalendar baseCalendar1 = connection.calendarService()
				.findBaseCalendar();
		logger.info("adjustments :" + baseCalendar1.adjustmentCount());
		Assert.assertEquals(baseCalendar.adjustmentCount(), 1);

		// now discard it
		baseCalendar.setDiscarded(true);
		connection.calendarService()
				.save(baseCalendar);
		// 
		// new calendar will be created because other one discarded
		baseCalendar = connection.calendarService()
				.findBaseCalendar();
		Assert.assertEquals(baseCalendar.adjustmentCount(), 0);

		// and a deleteAll
		baseCalendar = new BaseCalendar(connection);
		baseCalendar.addAdjustment(2008, 5, 4, true, 5);
		testdao.save(baseCalendar);
		baseCalendar = new BaseCalendar(connection);
		testdao.save(baseCalendar);
		baseCalendar.addAdjustment(2008, 5, 4, true, 6);
		// don't forget one added already as part of db setup
		Assert.assertEquals(connection.calendarService()
				.countBaseCalendars(), 3);
		Assert.assertEquals(connection.calendarService()
				.countBaseCalendarsDiscarded(), 1);

		connection.calendarService()
				.deleteAllBaseCalendars();
		// 

		// initially there won;t be any basecalendars in the database
		Assert.assertEquals(connection.calendarService()
				.countBaseCalendars(), 0);
		// But then there should still be one after a call to find because a new one is created automatically
		// if none found in the database

		baseCalendar = connection.calendarService()
				.findBaseCalendar();
		Assert.assertEquals(connection.calendarService()
				.countBaseCalendars(), 1);
		// and it should be a fresh one
		Assert.assertEquals(baseCalendar.adjustmentCount(), 0);

	}
}
